package com.cci.kangdao.wechatOrder.dao;

import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.wechatOrder.model.WeChatUserRelationT;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

/**
 * @description: 微信用户关联
 * @author: zyf
 * @create: 2021-03-25
 */
@MapperScan
public interface WeChatUserRelationTDao {

    /**
     * 根据openid查询用户信息
     * @param openId
     * @return
     */
    @Select("select top 1 UT.* from User_T UT " +
            "inner join WeChatUserRelation_T WCURT " +
            "on UT.ID = WCURT.UserID " +
            "where WCURT.OpenID = #{openId} and UT.Flag =0 and WCURT.CompanyID = #{companyID}  and WCURT.Flag =0 order by WCURT.CreateTime desc")
    UserT selectUserByOpenId(@Param("openId") String openId,@Param("companyID") Long companyID);

    @Select("select top 1 * from WeChatUserRelation_T where OpenID = #{openId} and CompanyID = #{companyID} and Flag =0 order by CreateTime desc")
    WeChatUserRelationT selectByOpenId(@Param("openId") String openId,@Param("companyID") Long companyID);


    @Select("select top 1 * from WeChatUserRelation_T where UserId = #{userId} and CompanyID = #{companyID}   and Flag =0 order by CreateTime desc")
    WeChatUserRelationT selectByUserIdAndCompanyID(@Param("userId") Long userId, @Param("companyID") Long companyID);

    @Select("select top 1  * from WeChatUserRelation_T where UserId = #{userId} and CompanyID = #{companyID} and Flag =0 order by CreateTime desc")
    WeChatUserRelationT selectByUserId(@Param("userId") Long userId,@Param("companyID")Long companyID);

    /**
     *
     * @param openId
     * @return
     */
    @Select("select top 1 * from WeChatUserRelation_T where OpenID = #{openId} and companyID=#{companyID}  and Flag =0 order by CreateTime desc")
    WeChatUserRelationT selectByOpenIdNotCompanyId(@Param("openId") String openId, @Param("companyID") Long companyID);

    /**
     * 根据open查询数据，动态传值flag
     * @param openId
     * @param flag
     * @return
     */

    WeChatUserRelationT selectRelationTByOpenIdAndFlag(@Param("userId") Long userId,@Param("openId") String openId,@Param("companyID")Long companyID,@Param("flag")Integer flag);

    /**
     * 根据open查询数据，动态传值flag
     * @param userId
     * @param flag
     * @return
     */
    List<WeChatUserRelationT> selectRelationTByOpenIdAndFlagList(@Param("userId") Long userId, @Param("flag") Integer flag);


    /**
     * 更新数据状态根据openId
     * @param userId
     * @param openId
     * @param flag
     * @return
     */
    /**
     * 更新数据状态根据openId
     * @param weChatUserRelationT
     * @return
     */
    int updateRelationTStatusByOpenId(WeChatUserRelationT weChatUserRelationT );

    /**
     *
     * @param weChatUserRelationT
     * @return
     */
    int insert(WeChatUserRelationT weChatUserRelationT);

    @Select("select top 1 * from User_T a\n" +
            "inner join WeChatUserRelation_T b on a.ID = b.UserID\n" +
            "where a.Phone = #{phone} and a.Flag = 0 and b.Flag = 0")
    Map<String, Object> getWeChatUserRelationTByUserPhone(@Param("phone") String phone);

    /**
     * 更新数据状态根据openId
     * @param userId
     * @param openId
     * @param flag
     * @return
     */
    /**
     * 更新数据状态根据openId
     * @param weChatUserRelationT
     * @return
     */
    int updateStatusByOpenId(WeChatUserRelationT weChatUserRelationT );
}
